package com.ruoyi.controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sign.Base64;
import com.ruoyi.pojo.ProcessDefinitionEntity;
import org.activiti.api.process.model.builders.DeleteProcessPayloadBuilder;
import org.activiti.api.process.model.payloads.DeleteProcessPayload;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.runtime.shared.query.Order;
import org.activiti.api.runtime.shared.query.Page;
import org.activiti.api.runtime.shared.query.Pageable;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.impl.util.IoUtil;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipInputStream;

@RestController
@RequestMapping("activiti/deployment")
public class DeploymentController extends BaseController {

    @Resource
    private RepositoryService repositoryService;
    @Resource
    private ProcessRuntime processRuntime;
    @PostMapping("deploy")
    public AjaxResult add (MultipartFile file) throws IOException {
        ZipInputStream zipInputStream = new ZipInputStream(file.getInputStream(), Charset.forName("UTF-8"));
        Deployment deploy = repositoryService.createDeployment()
                .addZipInputStream(zipInputStream).deploy();
        String id = deploy.getId();
        if (StringUtils.isBlank(id)){
            return AjaxResult.error("部署失败");
        }

        return AjaxResult.success("部署成功");
    }

    @GetMapping("list")
    public TableDataInfo list(@RequestParam("pageNum") Integer page, @RequestParam("pageSize") Integer size){
        List<ProcessDefinition> list1 = repositoryService.createProcessDefinitionQuery().list();
        List<ProcessDefinition> processDefinitions = repositoryService.createProcessDefinitionQuery().listPage((page-1)*size, size);
        List<ProcessDefinitionEntity> list = new ArrayList<>();
        processDefinitions.forEach(s -> {
            ProcessDefinitionEntity processDefinitionEntity = new ProcessDefinitionEntity();
            processDefinitionEntity.setId(s.getId());
            processDefinitionEntity.setName(s.getName());
            processDefinitionEntity.setDeploymentId(s.getDeploymentId());
            processDefinitionEntity.setKey(s.getKey());
            processDefinitionEntity.setDiagramResourceName(s.getDiagramResourceName());
            processDefinitionEntity.setSourceName(s.getResourceName());
            processDefinitionEntity.setVersion(s.getVersion());

            InputStream resourceAsStream = repositoryService.getResourceAsStream(s.getDeploymentId(), s.getDiagramResourceName());
            byte[] bytes = IoUtil.readInputStream(resourceAsStream, "");
            processDefinitionEntity.setImage(Base64.encode(bytes));
            list.add(processDefinitionEntity);
        });

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(list1.size());
        return rspData;
    }


    @DeleteMapping("remove/{id}")
    public AjaxResult remove(@PathVariable String id){
        repositoryService.deleteDeployment(id);
        return AjaxResult.success("删除成功");
    }



}
